package com.linewell.license.demo.utils;

import com.linewell.license.demo.model.User;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
* @author
* Company:
* Createtime : 2018年8月30日 下午1:45:20
* Description : 当前请求相关工具
*/
public class CurrentRequestUtils {

	private final static String SESSION_KEY = "LOGIN-INFO";
    /**
     * 永久重定向
     */
	private final static int SC_PERMANENT_REDIRECT = 308;

	/**
     **获取HttpServletRequest
     * @return
     */
    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     **获取HttpServletResponse
     * @return
     */
    public static HttpServletResponse getResponse() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }

    /**
     ** 获取HttpSession
     * @return
     */
    public static HttpSession getSession() {
        return CurrentRequestUtils.getRequest().getSession();
    }


    /**
     ** 获取登录信息
     * @return
     */
    public static User getLoginInfo(HttpServletRequest request ) {
        return (User) request.getSession().getAttribute(SESSION_KEY);
    }


    /**
     ** 获取登录信息
     * @return
     */
    public static User getLoginInfo() {
        return (User) CurrentRequestUtils.getRequest().getSession().getAttribute(SESSION_KEY);
    }

    /**
     ** 获取登录信息
     * @return
     */
    public static void setLoginInfo(User info) {
        CurrentRequestUtils.getRequest().getSession().setAttribute(SESSION_KEY , info);
    }
    /**
     **判断http请求是否Ajax请求
     * @param request 请求参数
     * @return
     */
    public static boolean isAjaxRequest(HttpServletRequest request) {
        String xRequestedWith = request.getHeader("X-Requested-With");
        return !StringUtils.isEmpty(xRequestedWith);
    }

    /**
     ** 判断http请求是否Ajax请求
     * @return
     */
    public static boolean isAjaxRequest() {
        return isAjaxRequest(getRequest());
    }

    /**
     * 获取访问根路径
     * @param request
     * @return
     */
    public static String getRequestContextPath(HttpServletRequest request){
        String host = request.getHeader("HTTP-HOST");
        if(StringUtils.isEmpty(host)) {
            host = request.getHeader("Host");
        }
        String scheme = request.getHeader("Proto");
        if(StringUtils.isEmpty(scheme)) {
            scheme = request.getScheme();
        }
        return scheme + "://" + host + request.getContextPath();
    }


    /**
     ** 对于请求是ajax请求重定向问题的处理方法
     * @param request 请求参数
     * @param response 返回参数
     * @param targetUrl 跳转地址
     * @throws IOException
     */
    public static void sendRedirect(HttpServletRequest request, HttpServletResponse response, String targetUrl) throws IOException{
        //获取当前请求的路径
        if(!isAjaxRequest(request)){
            response.sendRedirect(CurrentRequestUtils.getRequestContextPath(request) + targetUrl);
            return;
        }
        //告诉ajax这是重定向
        response.setHeader("REDIRECT", "REDIRECT");
        //重定向地址
        response.setHeader("CONTEXTPATH", CurrentRequestUtils.getRequestContextPath(request)+targetUrl);
        response.setStatus(CurrentRequestUtils.SC_PERMANENT_REDIRECT);
    }

}
